/*j07c8riwYnz20MYibuDbtKX3aAHwVScUH0o1jr9bLDbrPowwFtbWCjxiLloLZa12*/
import { cardCache } from 'nc-lightapp-front';
import constants from '../../constants';
import initTemplate from './initTemplate';
import setStatus from './setStatus';
import { accbookSwitchHandler } from './accbookHandler';

const { assetDataSource, UISTATE } = constants;

//翻页处理
export default function(props, pk) {
	const that = this;

	//清空数据
	this.resetOnAdd();

	if (!pk) {
		setStatus.call(that, props, UISTATE.BLANK);
		return;
	}

	// 更新参数
	props.setUrlParam({
		id: pk,
		status: UISTATE.BROWSE
	});

	let pk_card_new = this.getPkcardFromUrl();
	let pk_accbook_new = this.getPkaccbookByPkcardHis();

	if (pk_card_new === this.curr_pk_card) {
		//新的卡片与当前卡片相同，只是账簿不同，切换账簿即可
		accbookSwitchHandler.call(this, pk_accbook_new);
		return;
	}
	let assetData = undefined;
	this.state.currentPkaccbook = pk_accbook_new;

	loadDataByPk(this, pk_card_new)
		.then((data) => {
			assetData = data;
			if (data.heads && data.heads.length) {
				const transiType = that.getHeadAttributeValue(data, 'transi_type');
				props.setUrlParam({ transiType });
				return initTemplate.call(that, props);
			}
		})
		.then(() => {
			that.setValue(assetData);
			this.updateAssetCache(assetData, 'update');
		});
}

function loadDataByPk(that, pkcard) {
	return new Promise((resolve, reject) => {
		let cacheData = cardCache.getDefData(pkcard, assetDataSource);
		if (cacheData) {
			resolve(cacheData);
		} else {
			resolve(that.getData(pkcard));
		}
	});
}

/*j07c8riwYnz20MYibuDbtKX3aAHwVScUH0o1jr9bLDbrPowwFtbWCjxiLloLZa12*/